package com.uzai.console.service.uzaichat.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.uzaichat.kefutalk.KefuTalkIdDto;
import com.uzai.console.dto.uzaichat.kefutalk.KefuTalkIdListDto;
import com.uzai.console.dto.uzaichat.kefutalk.KefuTalkInfoDto;
import com.uzai.console.dto.uzaichat.kefutalk.KefuTalkQuery;
import com.uzai.console.dto.uzaichat.kefutalktype.KefuTalkTypeIdDto;
import com.uzai.console.dto.uzaichat.kefutalktype.KefuTalkTypeInfoDto;
import com.uzai.console.dto.uzaichat.kefutalktype.KefuTalkTypeQuery;
import com.uzai.console.entity.KefuTalk;
import com.uzai.console.entity.KefuTalkType;
import com.uzai.console.mapper.KefuTalkTypeMapper;
import com.uzai.console.mapper.KefuTalkMapper;
import com.uzai.console.mapper.WechatUserMapper;
import com.uzai.console.service.uzaichat.KefuTalkService;
import com.uzai.console.vo.uzaichat.kefutalk.KefuTalkInfoVo;
import com.uzai.console.vo.uzaichat.kefutalktype.KefuTalkTypeInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuqi
 * @date 2021年09月01日 19:26
 */
@Service
public class KefuTalkServiceImpl implements KefuTalkService {

    @Autowired
    private KefuTalkMapper kefuTalkMapper;
    @Autowired
    private KefuTalkTypeMapper kefuTalkTypeMapper;
    @Autowired
    private WechatUserMapper wechatUserMapper;

    /**
     * 分页查询快捷短语列表
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param kefuTalkQuery
     * @return null
     */
    public Page<KefuTalkInfoVo> findKefuTalkList(KefuTalkQuery kefuTalkQuery){
        //新建返回到页面的LIST对象
        List<KefuTalkInfoVo> kefuTalkInfoVoList = new ArrayList<>();

        //执行分页查询对象
        Page<KefuTalk> page = new Page<>();
        kefuTalkQuery.convert(page);
        List<KefuTalk> kefuTalkList = kefuTalkMapper.findByList(page, kefuTalkQuery);

        if(kefuTalkList != null){
            for (KefuTalk kefuTalk : kefuTalkList){
                KefuTalkInfoVo kefuTalkInfoVo = new KefuTalkInfoVo();
                BeanUtils.copyProperties(kefuTalk, kefuTalkInfoVo);
                //快捷短语分类名称
                KefuTalkType kefuTalkType = kefuTalkTypeMapper.selectById(kefuTalk.getTalkType(), kefuTalkQuery.getMerId());
                if(kefuTalkType != null){
                    kefuTalkInfoVo.setTalkTypeName(kefuTalkType.getName());
                    kefuTalkInfoVo.setTalkTypeSort(kefuTalkType.getSort());
                }
                kefuTalkInfoVoList.add(kefuTalkInfoVo);
            }
        }

        //返回到页面分页对象
        Page<KefuTalkInfoVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(kefuTalkInfoVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 查询快捷短语详情
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param kefuTalkIdDto
     * @return null
     */
    public KefuTalkInfoVo findKefuTalkInfo(KefuTalkIdDto kefuTalkIdDto){
        if(kefuTalkIdDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        //查询页面用户
        KefuTalk kefuTalk = kefuTalkMapper.selectById(kefuTalkIdDto.getId(), kefuTalkIdDto.getMerId());

        if(kefuTalk == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该快捷短语不存在");
        }

        KefuTalkInfoVo kefuTalkInfoVo =  new KefuTalkInfoVo();

        BeanUtils.copyProperties(kefuTalk, kefuTalkInfoVo);

        //快捷短语分类名称
        KefuTalkType kefuTalkType = kefuTalkTypeMapper.selectById(kefuTalk.getTalkType(), kefuTalkIdDto.getMerId());
        if(kefuTalkType != null){
            kefuTalkInfoVo.setTalkTypeName(kefuTalkType.getName());
            kefuTalkInfoVo.setTalkTypeSort(kefuTalkType.getSort());
        }

        return kefuTalkInfoVo;
    }

    /**
     * 添加快捷短语
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param kefuTalkInfoDto
     * @return null
     */
    @Transactional
    public void addKefuTalk(KefuTalkInfoDto kefuTalkInfoDto){
        if(kefuTalkInfoDto.getTalkType() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择短语分类");
        }

        if(kefuTalkInfoDto.getSort() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入短语排序");
        }

        if(StringUtils.isBlank(kefuTalkInfoDto.getTalkTitle())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入短语标题");
        }

        if(StringUtils.isBlank(kefuTalkInfoDto.getTalkContent())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入短语内容");
        }

        KefuTalk kefuTalk = new KefuTalk();
        BeanUtils.copyProperties(kefuTalkInfoDto, kefuTalk);
        Integer now = DateUtil.getNowTime();
        kefuTalk.setId(IdWorker.getId());
        kefuTalk.setContentType(Tools.getInteger(kefuTalkInfoDto.getContentType()));
        kefuTalk.setCreateTime(now);
        kefuTalk.setUpdateTime(now);
        kefuTalkMapper.insertSelective(kefuTalk);
    }

    /**
     * 修改快捷短语
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param kefuTalkInfoDto
     * @return null
     */
    @Transactional
    public void updateKefuTalk(KefuTalkInfoDto kefuTalkInfoDto){

        if(kefuTalkInfoDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入短语id");
        }

        if(kefuTalkInfoDto.getTalkType() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择短语分类");
        }

        if(kefuTalkInfoDto.getSort() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入短语排序");
        }

        if(StringUtils.isBlank(kefuTalkInfoDto.getTalkTitle())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入短语标题");
        }

        if(StringUtils.isBlank(kefuTalkInfoDto.getTalkContent())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入短语内容");
        }

        KefuTalk kefuTalk = kefuTalkMapper.selectById(kefuTalkInfoDto.getId(), kefuTalkInfoDto.getMerId());

        if(kefuTalk == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该快捷短语不存在");
        }

        kefuTalk.setTalkType(kefuTalkInfoDto.getTalkType());
        kefuTalk.setSort(kefuTalkInfoDto.getSort());
        kefuTalk.setTalkTitle(kefuTalkInfoDto.getTalkTitle());
        kefuTalk.setTalkContent(kefuTalkInfoDto.getTalkContent());
        kefuTalk.setContentType(Tools.getInteger(kefuTalkInfoDto.getContentType()));
        kefuTalk.setUpdateTime(DateUtil.getNowTime());
        kefuTalkMapper.updateById(kefuTalk);
    }


    /**
     * 删除快捷短语
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param kefuTalkIdListDto
     * @return null
     */
    @Transactional
    public void deleteKefuTalk(KefuTalkIdListDto kefuTalkIdListDto){

        if(kefuTalkIdListDto.getIdList() == null || kefuTalkIdListDto.getIdList().size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择短语id");
        }

        //删除
        for (Long id : kefuTalkIdListDto.getIdList()) {
            kefuTalkMapper.deleteById(id, kefuTalkIdListDto.getMerId());
        }

    }

    /**
     * 分页查询快捷短语类型列表
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param kefuTalkTypeQuery
     * @return null
     */
    public Page<KefuTalkTypeInfoVo> findKefuTalkTypeList(KefuTalkTypeQuery kefuTalkTypeQuery){
        //新建返回到页面的LIST对象
        List<KefuTalkTypeInfoVo> kefuTalkTypeInfoVoList = new ArrayList<>();

        //执行分页查询对象
        Page<KefuTalkType> page = new Page<>();
        kefuTalkTypeQuery.convert(page);
        List<KefuTalkType> kefuTalkTypeList = kefuTalkTypeMapper.findByList(page, kefuTalkTypeQuery);

        if(kefuTalkTypeList != null){
            for (KefuTalkType kefuTalkType : kefuTalkTypeList){
                KefuTalkTypeInfoVo kefuTalkTypeInfoVo = new KefuTalkTypeInfoVo();
                BeanUtils.copyProperties(kefuTalkType, kefuTalkTypeInfoVo);
                kefuTalkTypeInfoVoList.add(kefuTalkTypeInfoVo);
            }
        }

        //返回到页面分页对象
        Page<KefuTalkTypeInfoVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(kefuTalkTypeInfoVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 添加快捷短语类型
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param kefuTalkTypeInfoDto
     * @return null
     */
    @Transactional
    public void addKefuTalkType(KefuTalkTypeInfoDto kefuTalkTypeInfoDto){


        if(kefuTalkTypeInfoDto.getSort() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入类型排序");
        }

        if(StringUtils.isBlank(kefuTalkTypeInfoDto.getName())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入类型名称");
        }

        //查询该序号是否存在
        KefuTalkType kefuTalkType_old = kefuTalkTypeMapper.selectBySort(null, kefuTalkTypeInfoDto.getSort(), kefuTalkTypeInfoDto.getMerId());
        if(kefuTalkType_old != null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "当前排序不能重复，请重新输入");
        }

        Integer now = DateUtil.getNowTime();
        KefuTalkType kefuTalkType = new KefuTalkType();
        kefuTalkType.setId(IdWorker.getId());
        kefuTalkType.setMerId(kefuTalkTypeInfoDto.getMerId());
        kefuTalkType.setName(kefuTalkTypeInfoDto.getName());
        kefuTalkType.setSort(kefuTalkTypeInfoDto.getSort());
        kefuTalkType.setCreateTime(now);
        kefuTalkType.setUpdateTime(now);
        kefuTalkTypeMapper.insert(kefuTalkType);
    }

    /**
     * 删除快捷短语类型
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param kefuTalkTypeIdDto
     * @return null
     */
    @Transactional
    public void deleteKefuTalkType(KefuTalkTypeIdDto kefuTalkTypeIdDto){
        if(kefuTalkTypeIdDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择会员id");
        }

        KefuTalkQuery kefuTalkQuery = new KefuTalkQuery();
        kefuTalkQuery.setMerId(kefuTalkTypeIdDto.getMerId());
        kefuTalkQuery.setTalkType(kefuTalkTypeIdDto.getId());

        List<KefuTalk> kefuTalkList = kefuTalkMapper.findByList(kefuTalkQuery);
        if(kefuTalkList != null && kefuTalkList.size() > 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分类下存在短语，不能删除");
        }

        kefuTalkTypeMapper.deleteById(kefuTalkTypeIdDto.getId(), kefuTalkTypeIdDto.getMerId());
    }

    /**
     * 修改快捷短语类型
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param kefuTalkTypeInfoDto
     * @return null
     */
    @Transactional
    public void updateKefuTalkType(KefuTalkTypeInfoDto kefuTalkTypeInfoDto){

        if(kefuTalkTypeInfoDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id参数");
        }

        if(kefuTalkTypeInfoDto.getSort() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入类型排序");
        }

        if(StringUtils.isBlank(kefuTalkTypeInfoDto.getName())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入类型名称");
        }

        KefuTalkType kefuTalkType = kefuTalkTypeMapper.selectById(kefuTalkTypeInfoDto.getId(), kefuTalkTypeInfoDto.getMerId());
        if(kefuTalkType == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该类型不存在");
        }

        //查询该序号是否存在
        KefuTalkType kefuTalkType_old = kefuTalkTypeMapper.selectBySort(kefuTalkTypeInfoDto.getId(), kefuTalkTypeInfoDto.getSort(), kefuTalkTypeInfoDto.getMerId());
        if(kefuTalkType_old != null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "当前排序不能重复，请重新输入");
        }

        Integer now = DateUtil.getNowTime();
        kefuTalkType.setName(kefuTalkTypeInfoDto.getName());
        kefuTalkType.setSort(kefuTalkTypeInfoDto.getSort());
        kefuTalkType.setUpdateTime(now);
        kefuTalkTypeMapper.updateById(kefuTalkType);
    }

}
